//
//  DownLoadViewController.m
//  CwinCar
//
//  Created by dlios on 15/10/20.
//  Copyright © 2015年 dlios. All rights reserved.
//

#import "DownLoadViewController.h"
#import "LowPriceCollectionCell.h"
#import "LowPriceGoodsModel.h"
#import "LowPriceViewController.h"
#import "PriceGoodsCell.h"
@interface DownLoadViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    NSInteger _count;
}

@property (nonatomic, retain) NSMutableDictionary *getDic;

@end

@implementation DownLoadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _count = 2;
    
    self.title = self.getDownModel.name;
    self.navigationItem.hidesBackButton = YES;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"fanhui"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
#pragma mark 建立商品详细collectionView
    UICollectionViewFlowLayout *flowout = [[UICollectionViewFlowLayout alloc] init];
    flowout.itemSize = CGSizeMake(mainScreen_Width / 3.4 , mainScreen_Width / 3.4);
    flowout.minimumInteritemSpacing = 0;
    flowout.minimumLineSpacing = 0;
    flowout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, mainScreen_Width, mainScree_Height - 64 ) collectionViewLayout:flowout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[PriceGoodsCell class] forCellWithReuseIdentifier:@"reuse"];
    
    [self creatCollectionViewData];
}

#pragma mark 请求商品数据信息
- (void)creatCollectionViewData
{
    self.getDic = [[NSMutableDictionary alloc]init];
    self.detailArr = [NSMutableArray array];
#pragma mark id拼接网址
    NSString *str = [NSString stringWithFormat:@"http://jjfz01.qianfan365.com/jjfz/front/product/list.do?custemcatsId=%@&currentPage=1&showCount=24",self.getDownModel.id];
    
    [AFNblock getdataWithString:str block:^(id block)
     {
         self.getDic = block;
//         NSLog(@"  awdawd  %@", self.getDic);
         for (NSDictionary *goodDic in [self.getDic objectForKey:@"product"])
         {
             LowPriceGoodsModel *goodModel = [LowPriceGoodsModel modelWithDictionary: goodDic];
             [self.detailArr addObject:goodModel];
         }
//            NSLog(@"%ld", self.detailArr.count);
         
//         self.collectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//             [self mainLoadMoreDate];
//         }];
         
         
         
         [self.collectionView reloadData];
     }];

}

- (void)mainLoadMoreDate
{
    
    NSMutableArray *arr = [[NSMutableArray alloc]init];
    
    NSString *str = [NSString stringWithFormat:@"http://jjfz01.qianfan365.com/jjfz/front/product/list.do?custemcatsId=%@&currentPage=%ld&showCount=24",self.getDownModel.id,(long)_count];
    
    [AFNblock getdataWithString:str block:^(id block)
     {
         NSMutableDictionary *loadDic = [[NSMutableDictionary alloc]init];
         loadDic = block;
         for (NSMutableDictionary *godDic in [loadDic objectForKey:@"product"])
         {
             LowPriceGoodsModel *dataModel = [[LowPriceGoodsModel alloc]initWithDictionary:godDic];
             [arr addObject:dataModel];
         }
         
         if (arr.count == 0)
         {
             [self.collectionView.footer noticeNoMoreData];
         }
         
         [self.detailArr addObjectsFromArray:arr];
         _count++;
         [self.collectionView reloadData];
     }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.collectionView.footer endRefreshing];
        [self.collectionView reloadData];
    });
}


#pragma mark collectionView点击跳转详情商品信息界面 并传值
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LowPriceViewController *webViewOfgoods = [[LowPriceViewController alloc]init];
    webViewOfgoods.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:webViewOfgoods animated:NO];
    LowPriceGoodsModel *getWebModel = [[LowPriceGoodsModel alloc]init];
    
    getWebModel = self.detailArr[indexPath.item];
    webViewOfgoods.WebModel = getWebModel;
}

#pragma mark collectionView协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.detailArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PriceGoodsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    LowPriceGoodsModel *goodsModel = self.detailArr[indexPath.item];
 
    [cell.PicImageView sd_setImageWithURL:[NSURL URLWithString:goodsModel.picurl]placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
    cell.BackImageView.image = [UIImage imageNamed:@"bg_share_large"];
    
    return cell;
}

#pragma mark 返回按钮实现
- (void)backAction:(UIBarButtonItem *)leftBarButtonItem
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
